package eduware.dozent;

import java.util.HashMap;

import eduware.ctrl.Ctrl;
import eduware.ctrl.MessageController.MoodDependantMessage;
import eduware.util.Config;
import eduware.util.WritesConfig;

/*********************************************
 * Class implementing the retirement of the prof
 * 
 * @author Maik
 * 
 */
public class Retirement implements WritesConfig {

	/*******************************
	 * internal counter variables
	 */
	public int moodCounter, sleepCounter = 0, hungerCounter = 0,
			thirstCounter = 0, toiletCounter = 0, overallCounter = 0;

	/****************************
	 * boolean variable to store the state of retirement
	 */
	public boolean retired = false;

	/*******************************
	 * internal counter to prevent that the user is informed every time a
	 * certein threshold is reached
	 */
	public int infoCnt = 51;

	/*******************************************
	 * consturctor to append the class to the config file
	 */
	public Retirement() {
		Config.getInstance().registerImplementedInterface(this);
	}

	/*****************
	 * returns whether the prof is already retired
	 * 
	 * @return boolean value of retirement
	 */
	public boolean isRetired() {
		return retired;
	}

	/****************
	 * check whether the prof will retire now based on bad care
	 */
	public void checkRetirement() {

		int i = 0;

		// MOOD
		if (MoodState.MOOD.getIndex() <= 0f) {
			moodCounter++;
			overallCounter++;
			i++;
		} else {
			// reduce the counter value to the fifth part
			moodCounter = moodCounter / 5;
		}

		// SLEEP
		if (MoodState.SLEEP.getIndex() <= 0f) {
			sleepCounter++;
			overallCounter++;
			i++;
		} else {
			// reduce the counter value to the fifth part
			sleepCounter = sleepCounter / 5;
		}

		// HUNGER
		if (MoodState.HUNGER.getIndex() <= 0f) {
			hungerCounter++;
			overallCounter++;
			i++;
		} else {
			// reduce the counter value to the fifth part
			hungerCounter = hungerCounter / 5;
		}

		// THIRST
		if (MoodState.THIRST.getIndex() <= 0f) {
			thirstCounter++;
			overallCounter++;
			i++;
		} else {
			// reduce the counter value to the fifth part
			thirstCounter = thirstCounter / 5;
		}

		// SLEEP
		if (MoodState.TOILET.getIndex() <= 0f) {
			toiletCounter++;
			overallCounter++;
			i++;
		} else {
			// reduce the counter value to the fifth part
			toiletCounter = toiletCounter / 5;
		}

		// check whether there were not more than two internal counters equal to
		// zero; divide by 2
		if (i < 3)
			overallCounter = overallCounter / 2;

		// check whether any of the internal counters is greater than 1000 or
		// the overall counter is greater than 3000
		if (moodCounter > 1000 || toiletCounter > 1000 || thirstCounter > 1000
				|| hungerCounter > 1000 || toiletCounter > 1000
				|| overallCounter > 3000) {
			/*System.out
					.printf("moodCounter: %4d - thirstCounter: %4d - hungerCounter: %4d - toiletCounter: %4d - overallCounter: %4d \n");*/
			this.earlyRetirement();
		}

		// check wheather one of the values nearly reaches the retirement threshold
		if (moodCounter > 800 || toiletCounter > 800 || thirstCounter > 800
				|| hungerCounter > 800 || toiletCounter > 800
				|| overallCounter > 2400) {
			// inform user
			if (infoCnt > 50) {
				infoCnt = 0;
				String ausg = "";
				try {
					ausg = ((MoodDependantMessage) Ctrl.getInstance()
							.getMessageController().getMessage("retirement"))
							.getMessage();
				} catch (Exception e) {
					e.printStackTrace();
				}
				MessageQueue.getInstance().addPrimaryMessage(ausg);
			}
			infoCnt++;
		}

	}

	/******************
	 * function to retire the prof
	 */
	public void earlyRetirement() {
		this.retired = true;
	}

	@Override
	public void writeConfig(HashMap<String, String> config) {
		String val = (this.retired) ? "true" : "false";
		config.put("retired", val);
	}

	public void load() {
		this.retired = (Config.getInstance().getString("retired", "false")
				.compareTo("false") == 0) ? false : true;
	}
}
